package org.lxw.stock;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Calendar;
import java.util.Date;

public class DayFileBlock implements Comparable<DayFileBlock> {
	final ByteBuffer bb1;

	public DayFileBlock(byte[] bs) {
		bb1 = ByteBuffer.wrap(bs);
		bb1.order(ByteOrder.LITTLE_ENDIAN);
	}

	public DayFileBlock(ByteBuffer slice) {
		bb1 = slice;
		bb1.order(ByteOrder.LITTLE_ENDIAN);
	}

	public DayFileBlock(int i) {
		this(new byte[32]);
		bb1.putInt(0, i);
	}

	public int getDate() {
		return bb1.getInt(0);
	}

	public float getOpen() {
		return bb1.getInt(4) / 100f;
	}

	public float getLow() {
		return bb1.getInt(8) / 100f;
	}

	public float getHigh() {
		return bb1.getInt(12) / 100f;
	}

	public float getClose() {
		return bb1.getInt(16) / 100f;
	}

	public float getLastClose() {
		return bb1.getInt(28) / 100f;
	}

	public int getTurnover() {
		return bb1.getInt(20);
	}

	public int getVolume() {
		return bb1.getInt(24);
	}

	@Override
	public String toString() {
		return getDate() + "," + getOpen() + "," + getLow() + "," + getHigh()
				+ "," + getClose() + "," + getTurnover() + "," + getVolume();
	}

	private float get(float f) {
		float t = (int) ((f + 0.001f) * 100f);
		return t / 100f;
	}

	@Override
	public int compareTo(DayFileBlock o) {
		return Long.signum(getDate() - o.getDate());
	}
}